package com.pang.songbo.songboexcel.web;

import com.pang.songbo.songboexcel.util.ExcelReader;
import com.pang.songbo.songboexcel.util.ExcelWriter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.util.*;

/**
 * @author Da.Pang
 * 2025/11/5 8:56
 */
@Controller
public class ExcelController {



    @RequestMapping("/file")
    public String file(Model model) {

        return "file";
    }

    /**
     * 文件上传具体实现方法;
     *
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    //@ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file , Model model) {
        if (!file.isEmpty()) {
           /* try {
                *//*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到d:/files大家是否能实现呢？ 等等;
                 * 这里只是简单一个例子,请自行参考，融入到实际中可能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名； 3、文件格式; 4、文件大小的限制;
                 *//*
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }*/


            try {
                String newPath = "D:/转换文件-"+System.currentTimeMillis()+".xlsx";
                method(file,newPath);

                model.addAttribute("message","上传成功！转换文件在地址："+newPath);
                //return "上传成功";
                return "index";
            } catch (Exception e) {
                e.printStackTrace();

                model.addAttribute("message","上传失败," + e.getMessage());

                return "index";
            }
        } else {
            model.addAttribute("message","上传失败，因为文件是空的.");

            return "index";

        }
    }


    /**
     * [描述]
     */
    private Object method(MultipartFile file ,String newPath ) throws  Exception{

        List<Map<String, Object>> maps = ExcelReader.readExcelStream(file.getInputStream(),file.getOriginalFilename(), 0);


        List<Map<String,Object>> result = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Map<String,Object> resultMap = new TreeMap<>();
            resultMap.putAll(map);
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if(entry.getKey() == null){
                    continue;
                }


                if(entry.getKey().contains("项目")){
                    if(entry.getValue() != null ){
                        String value = entry.getValue().toString();
                        String[] split = value.split("\\|");

                        for (int i = 0; i < split.length; i++) {
                            String newName = entry.getKey() + "-" + (i + 1);
                            resultMap.put(newName,split[i]);
                        }
                    }


                }
            }
            result.add(resultMap);
        }


        ExcelWriter.writeExcelWithMap(newPath,"测试",result);
        return null;
    }


}